package com.farm.forms.service.impl;

import java.util.ArrayList;

import com.farm.forms.domain.Query;
import com.farm.core.time.TimeTool;
import com.farm.forms.dao.QueryDaoInter;
import com.farm.forms.dao.RulesDaoInter;
import com.farm.forms.dao.TitleDaoInter;
import com.farm.forms.service.QueryServiceInter;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;

/* *
 *功能：数据查询器服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：王东
 *日期：20141124110140
 *说明：
 */
public class QueryServiceImpl implements QueryServiceInter {
	private QueryDaoInter queryDao;
	private RulesDaoInter rulesDao;
	private TitleDaoInter titleDao;

	@Override
	public Query insertQueryEntity(Query entity, LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setCusername(user.getName());
		entity.setEuser(user.getId());
		entity.setEusername(user.getName());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		entity.setManychoose("1");
		return queryDao.insertEntity(entity);
	}

	@Override
	public Query editQueryEntity(Query entity, LoginUser user) {
		Query entity2 = queryDao.getEntity(entity.getId());
		entity2.setEuser(user.getId());
		entity2.setEusername(user.getName());
		entity2.setEtime(TimeTool.getTimeDate14());
		entity2.setDefsortstr(entity.getDefsortstr());
		entity2.setDefsorttype(entity.getDefsorttype());
		entity2.setPagesize(entity.getPagesize());
		entity2.setDistinctable(entity.getDistinctable());
		entity2.setCachetime(entity.getCachetime());
		entity2.setReportmaxnum(entity.getReportmaxnum());
		entity2.setName(entity.getName());
		entity2.setFromstr(entity.getFromstr());
		entity2.setSelectstr(entity.getSelectstr());
		entity2.setPcontent(entity.getPcontent());
		entity2.setReportable(entity.getReportable());
		entity2.setFitcolumns(entity.getFitcolumns());
		entity2.setNowrap(entity.getNowrap());
		entity2.setDefaultrule(entity.getDefaultrule());
		queryDao.editEntity(entity2);
		return entity2;
	}

	@Override
	public void deleteQueryEntity(String id, LoginUser user) {
		rulesDao.deleteEntitys(DBRule.addRule(new ArrayList<DBRule>(),
				"QUERYID", id, "="));
		titleDao.deleteEntitys(DBRule.addRule(new ArrayList<DBRule>(),
				"QUERYID", id, "="));
		queryDao.deleteEntity(queryDao.getEntity(id));
	}

	@Override
	public Query getQueryEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return queryDao.getEntity(id);
	}

	@Override
	public DataQuery createQuerySimpleQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery
				.init(
						query,
						"FARM_FORMS_QUERY a left join FARM_FORMS_CATEGORY b on a.TYPEID=b.ID",
						"a.ID as ID,a.DEFSORTSTR as DEFSORTSTR,a.DEFSORTTYPE as DEFSORTTYPE,a.PAGESIZE as PAGESIZE,a.DISTINCTABLE as DISTINCTABLE,a.CACHETIME as CACHETIME,a.TYPEID as TYPEID,a.REPORTMAXNUM as REPORTMAXNUM,a.NAME as NAME,a.REPORTABLE as REPORTABLE,b.name as TYPENAME");
		return dbQuery;
	}

	// ----------------------------------------------------------------------------------
	public QueryDaoInter getQueryDao() {
		return queryDao;
	}

	public void setQueryDao(QueryDaoInter dao) {
		this.queryDao = dao;
	}

	public RulesDaoInter getRulesDao() {
		return rulesDao;
	}

	public void setRulesDao(RulesDaoInter rulesDao) {
		this.rulesDao = rulesDao;
	}

	public TitleDaoInter getTitleDao() {
		return titleDao;
	}

	public void setTitleDao(TitleDaoInter titleDao) {
		this.titleDao = titleDao;
	}

}
